package com.org.core.constant;

import java.io.Serializable;
import java.util.List;

import net.sf.json.JSONArray;

import org.apache.commons.lang.StringUtils;

public class Pager implements Serializable {
	private static final long serialVersionUID = -6725435318909662218L;
	private Integer currentPage; // 当前页码
	private Integer totalCount; // 符合记录的总数
	private Integer totalPages; // 总页数
	private Integer begin; // 从begin开始显示页码
	private Integer end; // 页码显示结束到end
	private Integer prevPage; // 前一页的页码
	private Integer nextPage; // 后一页的页码
	private Integer firstPage = 1; // 第一页
	private Integer lastPage; // 最后一页码
	private Integer pageCount = 15; // 每页显示数量
	private Integer start = 0; // 这条记录开始查询
	private String orderBy; // 排序字段
	private String sc; // 排序方式 （默认desc
	private JSONArray resultArray;
	private List<?> resultList;
	private final Integer UNIT = 3; // 页码显示的跨度
	private String url;
	
	public Pager(String currentPage) {
		if(StringUtils.isEmpty(currentPage)) {
			currentPage = "1";
		}
		this.currentPage = Integer.valueOf(currentPage);
	}

	public Integer getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public Integer getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(Integer totalCount) {
		this.totalCount = totalCount;
	}

	public Integer getTotalPages() {
		if(totalCount == null) {
			throw new RuntimeException("分页查询：未计算总记录数");
		}
		if(totalPages == null) {
			totalPages = (totalCount % pageCount == 0) ? (totalCount / pageCount) : ((totalCount / pageCount) + 1); 
		}
		return totalPages;
	}

	public Integer getBegin() {
		begin = currentPage - UNIT;
		begin = (begin <= 1) ? 1 : (begin);
		return begin;
	}

	public Integer getEnd() {
		Integer totalPages = getTotalPages();
		if(totalPages <= 0) {
			return 0;
		}
		end = currentPage + UNIT;
		end = end >= (totalPages-1) ? (totalPages-1) : end;
		return end;
	}

	public Integer getPrevPage() {
		prevPage = currentPage - 1;
		return prevPage;
	}

	public Integer getNextPage() {
		nextPage = currentPage + 1;
		return nextPage;
	}

	public Integer getFirstPage() {
		return firstPage;
	}

	public Integer getLastPage() {
		lastPage = totalPages;
		return lastPage;
	}

	public Integer getPageCount() {
		return pageCount;
	}

	public void setPageCount(Integer pageCount) {
		this.pageCount = pageCount;
	}

	public Integer getStart() {
		this.start = (getCurrentPage() - 1) * getPageCount();
		return start;
	}

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public String getSc() {
		// 默认desc
		if(sc == null) {
			sc = "desc";
		}
		return sc;
	}

	public void setSc(String sc) {
		this.sc = sc;
	}

	public JSONArray getResultArray() {
		return resultArray;
	}

	public void setResultArray(JSONArray resultArray) {
		this.resultArray = resultArray;
	}

	public List<?> getResultList() {
		return resultList;
	}

	public void setResultList(List<?> resultList) {
		this.resultList = resultList;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	
}
